Although OPC Data Client can be used from Python over the COM API, as described in this article, this approach is now deprecated. The examples that use this approach are kept in the product for completeness only. With current OPC Data Client versions, you should install and import the Python Packages, which in turn use the Python.NET package to expose the OPC Data Client API to your Python code.
There is a comprehensive set of examples using this new approach. See OPC Studio .NET Examples.
|
With some earlier OPC Data Client versions, the examples were tested in Python 3.4.2, with pywin32-219 extension (Build 219). With OPC Data Client 2018.3, 2019.2, 2023.1 and 2023.2, the examples were tested in Python 3.6 (64-bit), and pywin32 (223) extension.
- DocExamples folder: Contains all Python examples for OPC Classic that are given in the Reference documentation.
- UADocExamples folder: Contains all Python examples for OPC-UA that are given in the Reference documentation.
In C:\PythonXX\Scripts, do:
pip install pypiwin32
The location can also be like
- C:\Users\userName\AppData\Local\Programs\Python\Python36-32\Scripts, or
- C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64\Scripts.
We were not able to make COM event notifications work in Python (COM). For this reason, examples that work with events do so using the
Event Pull Mechanism.
Using Python Examples from Visual Studio
Here are some pieces of advise related to this mode of usage we have figured out. You may be well accustomed with them... but we weren't.
- It is best to install Python runtime from the Visual Studio Installer. After pressing "Modify", switch to the "Individual components" tabs, and type "python" into the search box. In the default installation, the "Python language support" and "Python web support" under "Development activities" may already be checked, but under "Compilers, build tools, and runtime", an item like "Python 3 64-bit (3.9.7)" may be unchecked. Check the box, and perform the installation. An alternative approach: When the Python compiler is not present and you open a Python project in Visual Studio, you will be offered with a screen that leads to you Python installations. We have found that following this route is more difficult, and requires additional steps to work around problems, so we recommend against it.
- If you want to find where the Python is installed for your project: In Visual Studio Solution Explorer, navigate to your project node, and below it, click on the concrete environment node under "Python Environments". In the Properties window, the path to the Python will be in the "Full Path" property. You might need it to invoke the "pip install" under the "Scripts" subdirectory.
See Also